'use strict';

const Controller = require('egg').Controller;

class ListController extends Controller {
  //查找
  async listdata() {
    let {ctx}=this
   //获取全部列表数据分页
   //定义默认的分页内容
   let {page = 1, pagesize = 3}=ctx.request.body

   let res=await ctx.service.datalist.getdata(page*1,pagesize*1)
  //  //判断res
   if(res)
   {
     //获取成功
     ctx.body={
       data:res.list,
       msg:"获取成功",
       code:1,
       total:res.total
     }
   }
   else{
      //获取成功失败
      ctx.body={
        msg:"获取失败",
        code:0
      }
   }
  }  
  //增加
  async adddata()
  {
    let {ctx}=this
    let {title,nr,loginid}=ctx.request.body
    //判断费控验证
    if (!title || !nr || !loginid)
     {
      return ctx.body = {
        code: 1,
        msg: '参数不全'
      } 
    }
    let res=await ctx.service.datalist.adddata(title,nr,loginid)
    //判断res
    if(res.affectedRows==1)
    {
      ctx.body={
        code:1,
        msg:"增加成功",
      }
    }
  }
  //修改
  async updatalist()
  {
    let {ctx}=this
    let {id,title,nr}=ctx.request.body
    //判断费控验证
    if (!title || !nr || !id)
     {
      return ctx.body = {
        code: 1,
        msg: '参数不全'
      }  
    }
    let res=await ctx.service.datalist.updata(id,title,nr)
      //判断有没有修改成功
      if(res.affectedRows==1)
      {
        return ctx.body = {
          code: 1,
          msg: '修改成功'
        } 
      }
      else{
        return ctx.body = {
          code: 0,
          msg: '修改失败'
        } 
      }
  }
  //删除
  async sc ()
  {
    let {ctx}=this
    let {id} = ctx.query
    // 非空验证
    if (!id) {
        return ctx.body = {
            code: 1, 
            msg: '参数不全'
        }
    }
    let res = await ctx.service.datalist.del(id)
    //判断
    if(res.affectedRows=== 1)
    {
      ctx.body={
        code:0,
        msg:"删除成功了"
      }
    }
    else
    {
      ctx.body={
        code:1,
        mag:"删除失败"
      }
    }
  }
  //详情
   async xq ()
  {
    let {ctx}=this
    let {id} = ctx.request.body
  
    // 非空验证
    if (!id) {
        return ctx.body = {
            code: 1, 
            msg: '参数不全'
        }
    }
    let res = await ctx.service.datalist.xq(id)
    //判断
    if(res.length)
    {
      ctx.body={
        code:0,
        data:res[0]
      }
    }
    else
    {
      ctx.body={
        code:1,
        mag:"获取失败"
      }
    }
  }
}

module.exports = ListController;